'''批量插入到数据库中'''
import time as Time

import config
import db


def transform(data):
    new_data = []
    for item in data:
        Sno = item[0]
        SSno = 0
        weekstart = item[7]
        weekend = item[8]
        weekshow = str(weekstart) + "-" + str(weekend)
        weektype = item[11]
        if weektype == 0:
            feature = "1234"
            F1 = True
            F2 = True
            F3 = True
            F4 = True
            singleweek_bin = ""
            doubleweek_bin = ""
            for i in range(weekend - weekstart + 1):
                if i % 2 == 0:
                    singleweek_bin += '0'
                    doubleweek_bin += '1'
                else:
                    singleweek_bin += '1'
                    doubleweek_bin += '0'
            singleweek = int(singleweek_bin, 2)
            doubleweek = int(doubleweek_bin, 2)
        if weektype == 1:
            feature = "13"
            F1 = True
            F2 = False
            F3 = True
            F4 = False
            doubleweek = 0
            singleweek_bin = ""
            for i in range(weekend - weekstart + 1):
                if i % 2 == 0:
                    singleweek_bin += '0'
                else:
                    singleweek_bin += '1'
            singleweek = int(singleweek_bin, 2)
        if weektype == 2:
            feature = "24"
            F1 = False
            F2 = True
            F3 = False
            F4 = True
            singleweek = 0
            doubleweek_bin = ""
            for i in range(weekend - weekstart + 1):
                if i % 2 == 0:
                    doubleweek_bin += '1'
                else:
                    doubleweek_bin += '0'
            doubleweek = int(doubleweek_bin, 2)
        arruser = "宋鹏飞"
        times = item[12:]

        for time in times:
            new_item = (
                Sno, SSno, weekstart, weekend, weekshow, feature, F1, F2, F3, F4, singleweek, doubleweek, arruser, time)
            new_data.append(new_item)

    week_days = [1, 2, 3, 4, 5, 6, 7]
    new_datas = []
    for item in new_data:
        room_info, time_info = item[-1]
        classroom = room_info
        time_number = (time_info - 1) % (config.periods_per_day) + 1  # 计算时间号对应第几节课
        week_day = week_days[(time_info - 1) // (config.periods_per_day)]  # 计算时间号对应第几周
        jieci = ((int)(time_number * 2 - 1), (int)(time_number * 2))
        arrtime = Time.strftime('%Y-%m-%d %H:%M:%S', Time.localtime())
        new_item = item[:5] + (str(classroom[0]), week_day) + jieci + item[5:11] + jieci + item[11:12] + jieci + (
            arrtime, item[12:13])
        new_datas.append(new_item)

    return new_datas


def export_toDB(result):
    new_result = tuple(transform(result))
    db.InsertBatchToDB(new_result)
